package com.ai.chinamobileai.controller;

import com.ai.chinamobileai.service.IChatHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.function.Consumer;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class CustomerServiceController {

    @Autowired
    private ChatClient serviceClient;
    @Autowired
    private IChatHistoryService chatHistoryService;

    @GetMapping(value = "/service",produces = "text/html;charset=UTF-8")
    public Flux<String> stream(String prompt, String chatId){
        chatHistoryService.saveChatHistory("chat",chatId);
        return serviceClient.prompt(prompt)
                .advisors(new Consumer<ChatClient.AdvisorSpec>() {
                    @Override
                    public void accept(ChatClient.AdvisorSpec advisorSpec) {
                        advisorSpec.param(ChatMemory.CONVERSATION_ID,chatId);
                    }
                })
                .stream()
                .content();
    }
}